﻿using System.Collections.Generic;
using UnityEngine;

namespace BlockOS.ChunkStorage
{
    public class DummyStorage : AbstractChunkStorage
    {
        public Queue<(int, int, int)> tasks = new();

        public override void RequestChunkData(int worldX, int worldY, int worldZ)
        {
            tasks.Enqueue((worldX, worldY, worldZ));
        }

        public override void Update()
        {
            for (int i = 0; tasks.Count > 0 && i < 3; i++)
            {
                var (x, y, z) = tasks.Dequeue();
                var (pos, data) = MakeChunkData(x, y, z);
                onChunkDataLoad(pos, data);
            }
        }

        private ((int, int, int), ChunkData) MakeChunkData(int worldX, int worldY, int worldZ)
        {
            if (worldY != 0) return ((worldX, worldY, worldZ), null);

            var chunkData = new ChunkData();

            int[,] heightMap = new int[16, 16];
            for (int x = 0; x < 16; x++)
            {
                for (int z = 0; z < 16; z++)
                {
                    heightMap[x, z] = Random.Range(8, 12);
                }
            }

            chunkData.BatchedAddBlock(new BlockData("Default:Block/log", ("axis", "y")), VoxelMath.HeightMap(0, 15, 0, 15, heightMap));

            return ((worldX, worldY, worldZ), chunkData);
        }
    }
}
